package com.dhh.sky.activity;

import java.util.Arrays;
import com.dhh.sky.R;
import com.dhh.sky.SDAApplication;
import com.dhh.sky.util.Config;
import com.microsoft.live.LiveAuthClient;
import com.microsoft.live.LiveAuthException;
import com.microsoft.live.LiveAuthListener;
import com.microsoft.live.LiveConnectClient;
import com.microsoft.live.LiveConnectSession;
import com.microsoft.live.LiveStatus;
import android.accounts.AccountAuthenticatorActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class SDAActivity extends AccountAuthenticatorActivity implements View.OnClickListener{
    private SDAApplication app;
    private LiveAuthClient authClient;
    private Button bLoginn;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.navigator);
        
        initView();
        
        app = (SDAApplication) getApplication();
        authClient = new LiveAuthClient(app, Config.CLIENT_ID);
        app.setAuthClient(authClient);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        authClient.initialize(Arrays.asList(Config.SCOPES), new LiveAuthListener() {
            @Override
            public void onAuthError(LiveAuthException exception, Object userState) {
                showSignIn();
                showToast(exception.getMessage());
            }

            @Override
            public void onAuthComplete(LiveStatus status,LiveConnectSession session,Object userState) {
                if (status == LiveStatus.CONNECTED) {
                	bLoginn.setVisibility(View.GONE);
                    launchMainActivity(session);
                }else {
                	if(status != LiveStatus.CONNECTED) 
                		showSignIn();
                }
            }
        });
    }
    
    private void initView(){
    	bLoginn = (Button) this.findViewById(R.id.ng_login);
    	bLoginn.setOnClickListener(this);    	
    }

	@Override
	public void onClick(View v) {
		authClient.login(SDAActivity.this, Arrays.asList(Config.SCOPES),
				new LiveAuthListener() {
					@Override
					public void onAuthComplete(LiveStatus status,LiveConnectSession session, Object userState) {
						if (status == LiveStatus.CONNECTED) {
							bLoginn.setVisibility(View.GONE);
							launchMainActivity(session);
						} else {
							showToast("Login did not connect. Status is "+ status + ".");
						}
					}

					@Override
					public void onAuthError(LiveAuthException exception,Object userState) {
						showToast(exception.getMessage());
					}
				});
		bLoginn.setVisibility(View.GONE);
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}
	
	private void launchMainActivity(LiveConnectSession session) {
        assert session != null;
        app.setSession(session);
        app.setConnectClient(new LiveConnectClient(session));
        startActivity(new Intent(getApplicationContext(), FileListActivity.class));
    }
		
	private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
	
	private void showSignIn(){
		bLoginn.setVisibility(View.VISIBLE);
	}
}